$(function(){
	$('#tabbar-div').find('span').bind('click',function(){
		if($(this).attr('class') != 'tab-front'){
			$('#tabbar-div').find('span').attr('class','tab-back');
			$(this).attr('class','tab-front');
			
			$('table[id*=-table]').css('display','none');
			$('#'+$(this).attr('id')+'le').css('display','table');
		}
	});
	$('#is_promote').bind('click',function(){
		if($('#is_promote').attr('checked') == 'checked'){
			$('tr[name=cx_tr]').css('display','');
		}else{
			$('tr[name=cx_tr]').css('display','none');
		}
	});
});

function addAttr(attrNum){
	var num = $('#attr_val_'+attrNum).attr('num');
	num = parseInt(num) + 1;
	$('#attr_val_'+attrNum).attr('num',num);
	
	var attrs = $('tr[name=attr_'+attrNum+']');
	var tr = '';
	if(attrNum == 1){
		tr = '<tr name="attr_1">'
	    	+'<td class="label"><a onclick="javascript:subAttr(1,this,'+num+');" herf="javascript:void(0);">[-]</a></td>'
	    	+'<td>'
	    	+'   <input type="text" name="attr1[]" maxlength="10" size="10" num="'+num+'" onblur="blurAttr(1,'+num+',this);">'
	    	+'	 编号：<input type="text" maxlength="20" size="15" name="sn[]" onblur="checkSn(this);">'
	    	+'	 状态：<select  name="attrstate[]"><option value="1">销售</option><option value="0">停售</option></select>'
	    	+'</td>'
	    	+'</tr>';
	    	//相册页添加
	    	var phototr = '<tr id="p-a-'+num+'">'
		     	          +'<td class="label" name="'+num+'-a1"></td>'
	             		  +'<td>'
	             		  +'<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
	             		  +'<p class="up_img_title"><span class="require-field">*</span>正面</p>'
	             		  +'<p class="up_img_p"><img src="/sys-admin/images/ecshop_logo.gif" id="fp_'+num+'"/><input type="hidden" name="frontphoto[]" id="hid_fp_'+num+'"></p>'
	             		  +'<div class="up_img_oper" style="display:none;"><a href="javascript:popUpImg(\'fp_'+num+'\',\'hid_fp_'+num+'\');">上传</a></div>'
	             		  +'</div>'
	             		  +'<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
	             		  +'<p class="up_img_title"><span class="require-field">*</span>背面</p>'
	             		  +'<p class="up_img_p"><img src="/sys-admin/images/ecshop_logo.gif" id="bp_'+num+'"/><input type="hidden" name="backphoto[]" id="hid_bp_'+num+'"></p>'
	             		  +'<div class="up_img_oper" style="display:none;"><a href="javascript:popUpImg(\'bp_'+num+'\',\'hid_bp_'+num+'\');">上传</a></div>'
	             		  +'</div>'
	             		  +'<div class="cf"><input class="button" type="button" onclick="popOtherPhoto('+num+');" value="添加其他相框图 ">'
	          		      +'<input type="hidden" value="" name="otherphoto['+num+'][]">'
	             		  +'</div>'
	             		  +'<div id="other_photo_'+num+'">'
	             		  +'</div>'
	             		  +'</td>'
	                      +'</tr>';
	    	$('#photo-table').append(phototr);
	    	//添加库存
	    	if($('#attr_val_2')[0]){
	    		//属性2存在
	    		var attr2s = $('input[name="attr2[]"]');
		    	for(var i=0;i<attr2s.length;i++){
		    		var attr2num = $(attr2s[i]).attr('num');
		    		var attr2name = $(attr2s[i]).val();
		    		var stocktr = '<tr id="a1-'+num+'-a2-'+attr2num+'">'
		      				+'<td class="label"><input type="checkbox" onclick="selectStock(this,'+num+','+attr2num+')"><input type="hidden" value="0" name="selstock[]" id="hid_s_'+num+'_'+attr2num+'"><b name="'+num+'-a1"></b>-<b name="'+attr2num+'-a2">'+attr2name+'</b></td>'
			  				+'<td>数量<input type="text" size="10" maxlength="10" value="0" name="stock[]" onblur="checkStockNum(this);"></td>'
		   					+'</tr>';
		    		$('#stock-table').append(stocktr);
		    	}
	    	}else{
	    		//属性2不存在
	    		var stocktr = '<tr id="a1-'+num+'">'
  				+'<td class="label"><input type="checkbox" onclick="selectStock(this,'+num+',0)"><input type="hidden" value="0" name="selstock[]" id="hid_s_'+num+'_0"><b name="'+num+'-a1"></b></td>'
  				+'<td>数量<input type="text" size="10" maxlength="10" value="0" name="stock[]" onblur="checkStockNum(this);"></td>'
					+'</tr>';
		        $('#stock-table').append(stocktr);
	    	}
	    	
	    	
	    	
	}else if(attrNum == 2){
		tr = '<tr name="attr_2">'
			+'<td class="label"><a onclick="javascript:subAttr(2,this,'+num+');" herf="javascript:void(0);">[-]</a></td>'
			+'<td><input type="text" name="attr2[]" maxlength="10" size="10" num="'+num+'" onblur="blurAttr(2,'+num+',this);"></td>'
			+'</tr>';
		//添加库存
    	var attr1s = $('input[name="attr1[]"]');
    	for(var i=0;i<attr1s.length;i++){
    		var attr1num = $(attr1s[i]).attr('num');
    		var attr1name = $(attr1s[i]).val();
    		var stocktr = '<tr id="a1-'+attr1num+'-a2-'+num+'">'
      				+'<td class="label"><input type="checkbox" onclick="selectStock(this,'+attr1num+','+num+')"><input type="hidden" value="0" name="selstock[]" id="hid_s_'+attr1num+'_'+num+'"><b name="'+attr1num+'-a1">'+attr1name+'</b>-<b name="'+num+'-a2"></b></td>'
	  				+'<td>数量<input type="text" size="10" maxlength="10" value="0"  name="stock[]"></td>'
   					+'</tr>';
   			var attrstocks = $('tr[id*=a1-'+attr1num+']');
   			$(attrstocks[attrstocks.length-1]).after(stocktr);
    	}	
	}
	if(attrs[0]){
		$(attrs[attrs.length-1]).after(tr);
	}else{
		$('#attr_val_'+attrNum).after(tr);
	}
}

function subAttr(attrNum,obj,num){
	$(obj).parent().parent().remove();
	if(attrNum == 1){
		$('#p-a-'+num).remove();
		$('tr[id*=a1-'+num+']').remove();
	}else if(attrNum == 2){
		$('tr[id$=-a2-'+num+']').remove();
	}
}

function blurAttr(attrNum,num,obj){
	$('b[name='+num+'-a'+attrNum+']').html($(obj).val());
	if(attrNum == 1){
		$('td[name='+num+'-a1]').html($(obj).val());
	}
}

function enUpImg(obj){
	$(obj).find('div.up_img_oper').css('display','');
}
function leUpImg(obj){
	$(obj).find('div.up_img_oper').css('display','none');
}

function addProImg(){
	$('#up_pro_img').ajaxSubmit({
		success:function(html){
			html = $.trim(html);
		    if(html.indexOf('temp') > 0){
		    	var html = '<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
		    		+'<p class="up_img_title">产品图片</p>'
		    		+'<p class="up_img_p"><img src="'+html+'"/><input type="hidden" value="'+html+'" name="imgs[]"></p>'
		    		+'<div class="up_img_oper" style="display:none;"><a href="javascript:void(0);" onclick="deleteImg(this);">删除</a></div>'
		    		+'</div>';
		    	$('#pro_imgs').append(html);
   			    closeUpImg();
            }else{
            	alert('上传图片必须为jpg、gif、png,且小于1M!');
            }
		}
	});
}

function addOtherPhoto(num){
	$('#up_other_photo_'+num).ajaxSubmit({
		success:function(html){
			html = $.trim(html);
		    if(html.indexOf('temp') > 0){
		    	var html = '<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
		    		+'<p class="up_img_title">产品图片</p>'
		    		+'<p class="up_img_p"><img src="'+html+'"/><input type="hidden" value="'+html+'" name="otherphoto['+num+'][]"></p>'
		    		+'<div class="up_img_oper" style="display:none;"><a href="javascript:void(0);" onclick="deleteImg(this);">删除</a></div>'
		    		+'</div>';
		    	$('#other_photo_'+num).append(html);
   			    closeUpImg();
            }else{
            	alert('上传图片必须为jpg、gif、png,且小于1M!');
            }
		}
	});
}


function submitEditProduct(){
	//属性基本信息tab验证
	if(!checkForm()){
		$('#product-tab').click();
		return;
	}
	if($('#is_promote').attr('checked') == 'checked'){
		if(!Fun.requiredPriceCheck($('#promote_val'))){
			$('#product-tab').click();
			return;
		}
	}
	$('#edit_pro').submit();
}



function submitProduct(){
	//属性基本信息tab验证
	if(!checkForm()){
		$('#product-tab').click();
		return;
	}
	if($('#is_promote').attr('checked') == 'checked'){
		if(!Fun.requiredPriceCheck($('#promote_val'))){
			$('#product-tab').click();
			return;
		}
	}
	//属性tab验证
	var attrs1 = $('input[name="attr1[]"]');
	for(var i=0;i<attrs1.length;i++){
		if($.trim($(attrs1[i]).val())===''){
			$('#attr-tab').click();
			alert('所有属性名不能为空');
			return;
		}
	}
	var attrs2 = $('input[name="attr2[]"]');
	if(attrs2[0]){
		for(var i=0;i<attrs2.length;i++){
			if($.trim($(attrs2[i]).val())===''){
				$('#attr-tab').click();
				alert('所有属性名不能为空');
				return;
			}
		}
	}
	var sns = $('input[name="sn[]"]');
	if(sns[0]){
		for(var i=0;i<sns.length;i++){
			if(!Fun.isNumChar(sns[i])){
				$('#attr-tab').click();
				alert('所有编号非空且由数字字母组成');
				return;
			}
		}
	}
	
    //相册tab验证
	var frontphotos = $('input[name="frontphoto[]"]');
	for(var i=0;i<frontphotos.length;i++){
		if($.trim($(frontphotos[i]).val())===''){
			$('#photo-tab').click();
			alert('所有正面图片必选上传');
			return;
		}
	}
	 //库存tab验证
	var stocks = $('input[name="stock[]"]');
	for(var i=0;i<stocks.length;i++){
		if(!Fun.zIntCheck(stocks[i])){
			$('#stock-tab').click();
			alert('所有库存数量必选为整数');
			return;
		}
	}
	
	$('#add_pro').submit();
	
}

function checkStockNum(obj){
	if(!Fun.zIntCheck(obj)){
		alert('请输入正确的库存数量');
	}
	
}
function checkSn(obj){
	if(!Fun.isNumChar(obj)){
		alert('请输入非空且由数字字母组成的编号');
	}
}

function popOtherPhoto(num){
	$('div.pop-dailog').remove();
	var html='<div class="pop-dailog" style="display:none;height:'+$(document).height()+'px;">'
		   +'<div class="container">'
		   +'<div class="head">'
		   +'上传图片<div class="buttons"><b class="pop-close" onclick="closeUpImg();"></b></div>'
		   +'</div>'
		   +'<div class="pop-content">'
		   +'<form action="/sys-admin/action/common/upfile.php" method="post" enctype="multipart/form-data" id="up_other_photo_'+num+'">'
		   +'<input type="hidden" name="met" value="upimg"/>'
		   +'<input type="file" name="image" onchange="addOtherPhoto('+num+');"/>'
		   +'</form>'
		   +'</div>'
		   +'</div>'
		   +'</div>';
	$('body').append(html);
	var left = ($(document).width()-$('div.pop-dailog').find('div.container').width())/2;
	left = left>0?left:0;
	var top = ($(window).height() - 54)/2;
	top = top>0?top:0;
	top += $(document).scrollTop();
	$('div.pop-dailog').find('div.container').css('top',top+'px');
	$('div.pop-dailog').find('div.container').css('left',left+'px');
	$('div.pop-dailog').css('display','');
}


function popProImg(num){
	$('div.pop-dailog').remove();
	var html='<div class="pop-dailog" style="display:none;height:'+$(document).height()+'px;">'
		   +'<div class="container">'
		   +'<div class="head">'
		   +'上传图片<div class="buttons"><b class="pop-close" onclick="closeUpImg();"></b></div>'
		   +'</div>'
		   +'<div class="pop-content">'
		   +'<form action="/sys-admin/action/common/upfile.php" method="post" enctype="multipart/form-data" id="up_pro_img">'
		   +'<input type="hidden" name="met" value="upimg"/>'
		   +'<input type="file" name="image" onchange="addProImg();"/>'
		   +'</form>'
		   +'</div>'
		   +'</div>'
		   +'</div>';
	$('body').append(html);
	var left = ($(document).width()-$('div.pop-dailog').find('div.container').width())/2;
	left = left>0?left:0;
	var top = ($(window).height() - 54)/2;
	top = top>0?top:0;
	top += $(document).scrollTop();
	$('div.pop-dailog').find('div.container').css('top',top+'px');
	$('div.pop-dailog').find('div.container').css('left',left+'px');
	$('div.pop-dailog').css('display','');
}

function selectStock(obj,num1,num2){
	if($(obj).attr('checked') == 'checked'){
		$('#hid_s_'+num1+'_'+num2).val(1);
	}else{
		$('#hid_s_'+num1+'_'+num2).val(0);
	}
}

//库存管理页
function submitProductStock(){
	if($('input[name="stockid[]"]:checked').length < 1){
		alert('请选择要改变的库存');
		return;
	}
	var type = $('#sr_type').val();
	if(type == ''){
		alert('请选择库存操作类型');
		return;
	}
	var numstr = $('#sr_num').val();
	var num = parseInt(numstr)+'';
	
	if(numstr !== num || num < 1){
		alert('请填写大于零的有效数量');
		return;
	}
	
	$('#stock_pro').submit();
}



function popEditAttr1(id){
	if(id > 0){
		var name = $.trim($('#a_1_n_'+id).html());
		var sn = $.trim($('#a_1_sn_'+id).html());
		var sort = $.trim($('#a_1_s_'+id).html());
		var state = $('#a_1_state_'+id).attr('state');
		state = (state==='0'?0:1);
		var html = '名　称： <input type="text" maxlength="10" size="10" num="1" value="'+name+'" id="pop_attr_name">'
	 　　　　　　 +'<p>编　号：<input type="text" maxlength="20" size="15" value="'+sn+'" id="pop_attr_sn"></p>';
	 	if(state == 1){
	 		html += '<p>状　态：<select id="pop_attr_state"><option value="1" selected="selected">销售</option><option value="0">停售</option></select></p>';
	 	}else{
	 		html += '<p>状　态：<select id="pop_attr_state"><option value="1">销售</option><option value="0" selected="selected">停售</option></select></p>';
	 	}  
	 	html += '<p>排　序：<input type="text" maxlength="2" size="4" value="'+sort+'" id="pop_attr_sort"></p>';
	 	html += '<input class="button" type="button" value="修改 " onclick="editAttr1('+id+');">';
	    popDailog('属性修改',html,190);
	}
}





function editAttr1(id){
		if(!Fun.isEmpty($('#pop_attr_name'))){
			alert('属性名不能为空');
			return;
		}
		if(!Fun.isNumChar($('#pop_attr_sn'))){
			alert('编号非空且由数字字母组成');
			return;
		}
		if(!Fun.zIntCheck($('#pop_attr_sort'))){
			alert('排序必须为整数');
			return;
		}
		var name = $.trim($('#pop_attr_name').val());
		var sn = $.trim($('#pop_attr_sn').val());
		var sort = $.trim($('#pop_attr_sort').val());
		var state = $('#pop_attr_state').val();
		state = (state==='0'?0:1);
		
		var url = '/sys-admin/action/goods/product.php?met=editsubpro'
			       +'&id='+id
			       +'&name='+name
			       +'&sn='+sn
			       +'&sort='+sort
			       +'&state='+state
			       +'&t='+(new Date().getTime());
		$.get(url,null,function(result){
			if($.trim(result) === '1'){
				$('#a_1_n_'+id).html(name);
				$('#a_1_sn_'+id).html(sn);
				$('#a_1_s_'+id).html(sort);
				$('#a_1_state_'+id).attr('state',state);
				$('#a_1_state_'+id).html(state==1?'销售':'停售');
				closeUpImg();
				alert('修改成功');
			}else{
				alert('修改失败');
			}
		});
}



function popEditAttr2(id){
	if(id > 0){
		var name = $.trim($('#a_2_n_'+id).html());
		var sort = $.trim($('#a_2_s_'+id).html());
		var html = '名　称： <input type="text" maxlength="10" size="10" num="1" value="'+name+'" id="pop_attr_name">'
	 	html += '<p>排　序：<input type="text" maxlength="2" size="4" value="'+sort+'" id="pop_attr_sort"></p>';
	 	html += '<input class="button" type="button" value="修改 " onclick="editAttr2('+id+');">';
	    popDailog('属性修改',html,130);
	}
}


function editAttr2(id){
	if(!Fun.isEmpty($('#pop_attr_name'))){
		alert('属性名不能为空');
		return;
	}
	if(!Fun.zIntCheck($('#pop_attr_sort'))){
		alert('排序必须为整数');
		return;
	}
	var name = $.trim($('#pop_attr_name').val());
	var sort = $.trim($('#pop_attr_sort').val());
	var url = '/sys-admin/action/goods/product.php?met=editattr'
	       +'&id='+id
	       +'&name='+name
	       +'&sort='+sort
	       +'&t='+(new Date().getTime());
	$.get(url,null,function(result){
		if($.trim(result) === '1'){
			$('#a_2_n_'+id).html(name);
			$('#a_2_s_'+id).html(sort);
			closeUpImg();
			alert('修改成功');
		}else{
			alert('修改失败');
		}
	});
}

//添加属性1
function popAddAttr1(productId){
	if(productId > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="add_sub_pro">'
		   +'<input type="hidden" name="met" value="addsubpro"/>'
		   +'<input type="hidden" name="pid" value="'+productId+'"/>'
		   +'名　称： <input type="text" maxlength="10" size="10" num="1" id="pop_attr_name" name="name">'
	 　　　　 +'<p>编　号：<input type="text" maxlength="20" size="15" id="pop_attr_sn" name="sn"></p>'
		   +'<p>状　态：<select id="pop_attr_state" name="state"><option value="1" selected="selected">销售</option><option value="0">停售</option></select></p>'
		   +'<p>排　序：<input type="text" name="sort" maxlength="2" size="4" id="pop_attr_sort"></p>'
		   +'<p>正面图： <input type="file" name="image" id="front_img"/><br/>先只添加产品相册正面图，大小最好为800*800</p>'
		   +'<input class="button" type="button" value="添加 " onclick="addAttr1('+productId+');">'
		   +'</form>';
	 	popDailog('属性添加',html,190);
	}
}
function addAttr1(productId){
	if(productId > 0){
		if(!Fun.isEmpty($('#pop_attr_name'))){
			alert('属性名不能为空');
			return;
		}
		if(!Fun.isNumChar($('#pop_attr_sn'))){
			alert('编号非空且由数字字母组成');
			return;
		}
		if(!Fun.zIntCheck($('#pop_attr_sort'))){
			alert('排序必须为整数');
			return;
		}
		if(!Fun.isEmpty($('#front_img'))){
			alert('正面图不能为空');
			return;
		}
		
		$('#add_sub_pro').ajaxSubmit({
			success:function(html){
				html = $.trim(html);
				var json = eval('('+html+')');
				if(json.code == 1){
					var name = $.trim($('#pop_attr_name').val());
					var sn = $.trim($('#pop_attr_sn').val());
					var sort = $.trim($('#pop_attr_sort').val());
					var state = $('#pop_attr_state').val();
					state = (state==='0'?0:1);
					
					var trs = $('tr[name*="imgs_a1_"]');
					var html ='<tr name="tr_a1_'+json.id+'">'
						+'    <td class="label"><a href=javascript:delAttr1('+json.id+'); title="删除">[-]</a><strong id="a_1_n_'+json.id+'">'+name+'</strong></td>'
						+'    <td> 编号：<span  id="a_1_sn_'+json.id+'">'+sn+'</span>'
						+'    &nbsp;&nbsp;状态：'+(state==1?'<span id="a_1_state_'+json.id+'" state="1">销售</span>':'<span id="a_1_state_'+json.id+'" state="0">停售</span>')
						+'   	&nbsp;&nbsp; 排序： <span id="a_1_s_'+json.id+'">'+sort+'</span>'
						+'	<input class="button" type="button" value="修改 " onclick="popEditAttr1('+json.id+');">'
						+'	</td>'
						+'</tr>'
						+'<tr name="imgs_a1_'+json.id+'">'
						+'    <td class="label"><input class="button" type="button" value="添加图片 " onclick="onclick="addAppendOtherPhoto('+json.id+');""></td>'
						+'    <td id="op_imgs_'+json.id+'">'
						+'    <div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
						+'		<p class="up_img_title"><span class="require-field">*</span>正面</p>'
						+'		<p class="up_img_p"><img src="'+json.src+'" id="fp_'+json.id+'"/></p>'
						+'  	<div class="up_img_oper" style="display:none;"><a href="javascript:popEditFrontPhoto('+json.id+');">修改</a></div>'
						+'	</div>'
						+'    <div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);">'
						+'		<p class="up_img_title">背面</p>'
						+'		<p class="up_img_p"><img src="" id="bp_'+json.id+'"/></p>'
						+'  	<div class="up_img_oper" style="display:none;"><a href="javascript:delBackPhoto('+json.id+');">删除</a><a href="javascript:popEditBackPhoto('+json.id+');">修改</a></div>'
						+'	</div>'
						+'	</td>'
						+'</tr>';
					if(trs[0]){
						$(trs[trs.length-1]).after(html);
					}else{
						$('#a1_label').after(html);
					}
					closeUpImg();
					alert('添加成功');
				}else{
					alert('添加失败');
				}
			}
		});
	}
}
//添加属性2
function popAddAttr2(productId){
	if(productId > 0){
		var html = '名　称： <input type="text" maxlength="10" size="10" num="1" value="" id="pop_attr_name">'
		 	html += '<p>排　序：<input type="text" maxlength="2" size="4" value="" id="pop_attr_sort"></p>';
		 	html += '<input class="button" type="button" value="修改 " onclick="addAttr2('+productId+');">';
		popDailog('属性添加',html,130);
	}
}

function addAttr2(productId){
	if(productId > 0){
		if(!Fun.isEmpty($('#pop_attr_name'))){
			alert('属性名不能为空');
			return;
		}
		if(!Fun.zIntCheck($('#pop_attr_sort'))){
			alert('排序必须为整数');
			return;
		}
		var name = $.trim($('#pop_attr_name').val());
		var sort = $.trim($('#pop_attr_sort').val());
		var url = '/sys-admin/action/goods/product.php?met=addattr'
		       +'&pid='+productId
		       +'&name='+name
		       +'&sort='+sort
		       +'&t='+(new Date().getTime());
		$.get(url,null,function(result){
			result = $.trim(result);
			if($.trim(result) === '0'){
				alert('添加失败');
			}else{
				var trs = $('tr[name*="tr_a2_"]');
				var html ='<tr name="tr_a2_'+result+'">'
				    +'<td class="label"><a onclick="delAttr2('+result+')" href="javascript:;" title="删除">[-]</a> <strong id="a_2_n_'+result+'">'+name+'</strong></td>'
				    +'<td>'
				    +	'排序： <span id="a_2_s_'+result+'">'+sort+'</span>'
					+	'<input class="button" type="button" value="添加 " onclick="popEditAttr2('+result+');">'
					+'</td>'
				    +'</tr>';
				if(trs[0]){
					$(trs[trs.length-1]).after(html);
				}else{
					$('#a2_label').after(html);
				}
				closeUpImg();
				alert('添加成功');
			}
		});
	}
}



//删除属性2
function delAttr1(attrid){
	if(attrid > 0 && $('tr[name=tr_a1_'+attrid+']')[0]){
		if(confirm("确定要删除此属性?")){
			var url = '/sys-admin/action/goods/product.php?met=delsubpro'
			       +'&subid='+attrid
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('tr[name=tr_a1_'+attrid+']').remove();
					$('tr[name=imgs_a1_'+attrid+']').remove();
					alert('属性删除成功');
				}else if(result == '2'){
					alert('属性关联的库存大于0，无法删除');
				}else{
					alert('属性删除失败');
				}
			});
		}
	}
}

//删除属性2
function delAttr2(attrid){
	if(attrid > 0 && $('tr[name=tr_a2_'+attrid+']')[0]){
		if(confirm("确定要删除此属性?")){
			var url = '/sys-admin/action/goods/product.php?met=delattr'
			       +'&aid='+attrid
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('tr[name=tr_a2_'+attrid+']').remove();
					alert('属性删除成功');
				}else if(result == '2'){
					alert('属性关联的库存大于0，无法删除');
				}else{
					alert('属性删除失败');
				}
			});
		}
	}
}


//修改正面图
function popEditFrontPhoto(sproid){
	if(sproid > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="edit_front_photo">'
		   +'<input type="hidden" name="met" value="editfrontphoto"/>'
		   +'<input type="hidden" name="sproid" value="'+sproid+'"/>'
		   +'正面图： <input type="file" name="image" id="front_img"/>'
		   +'<br/><input class="button" type="button" value="修改 " onclick="editFrontPhoto('+sproid+');">'
		   +'</form>';
	 	popDailog('正面图修改',html,190);
	}
}
function editFrontPhoto(sproid){
	if(sproid > 0){
		if(!Fun.isEmpty($('#front_img'))){
			alert('正面图不能为空');
			return;
		}
		$('#edit_front_photo').ajaxSubmit({
			success:function(html){
				html = $.trim(html);
				var json = eval('('+html+')');
				if(json.code == 1){
					$('#fp_'+sproid).attr('src',json.src);
					closeUpImg();
					alert('修改成功');
				}else{
					alert('修改失败');
				}
			}
		});
	}
}

//修改背面图
function popEditBackPhoto(sproid){
	if(sproid > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="edit_Back_photo">'
		   +'<input type="hidden" name="met" value="editbackphoto"/>'
		   +'<input type="hidden" name="sproid" value="'+sproid+'"/>'
		   +'背面图： <input type="file" name="image" id="back_img"/>'
		   +'<br/><input class="button" type="button" value="修改 " onclick="editBackPhoto('+sproid+');">'
		   +'</form>';
	 	popDailog('背面图修改',html,190);
	}
}
function editBackPhoto(sproid){
	if(sproid > 0){
		if(!Fun.isEmpty($('#back_img'))){
			alert('背面图不能为空');
			return;
		}
		$('#edit_Back_photo').ajaxSubmit({
			success:function(html){
				html = $.trim(html);
				var json = eval('('+html+')');
				if(json.code == 1){
					$('#bp_'+sproid).attr('src',json.src);
					closeUpImg();
					alert('修改成功');
				}else{
					alert('修改失败');
				}
			}
		});
	}
}

function delBackPhoto(sproid){
	if(sproid > 0 && $('#bp_'+sproid).attr('src') != ''){
		if(confirm("确定要删除此背面图?")){
			var url = '/sys-admin/action/goods/product.php?met=delbackphoto'
			       +'&sproid='+sproid
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('#bp_'+sproid).attr('src','');
					alert('背面图删除成功');
				}else{
					alert('背面图删除失败');
				}
			});
		}
	}
}


function addAppendOtherPhoto(sproid){
	if(sproid > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="append_other_photo">'
		   +'<input type="hidden" name="met" value="appendotherphoto"/>'
		   +'<input type="hidden" name="sproid" value="'+sproid+'"/>'
		   +'正面图： <input type="file" name="image" id="other_photo"/><br/>大小最好为800*800'
		   +'<p>排　序：<input type="text" name="sort" maxlength="2" size="4" id="photo_sort"></p>'
		   +'<input class="button" type="button" value="添加 " onclick="appendOtherPhoto('+sproid+');">'
		   +'</form>';
	 	popDailog('相册图片添加',html,150);
	}
}

function appendOtherPhoto(sproid){
	if(sproid > 0){
		if(!Fun.zIntCheck($('#photo_sort')) && parseInt($('#photo_sort').val()) < 2){
			alert('排序必须为大于2的整数');
			return;
		}
		if(!Fun.isEmpty($('#other_photo'))){
			alert('图片不能为空');
			return;
		}
		$('#append_other_photo').ajaxSubmit({
			success:function(html){
				html = $.trim(html);
				var json = eval('('+html+')');
				if(json.code == 1){
					var sort = parseInt($('#photo_sort').val());
					var html = '<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);" id="op_div_'+sproid+'_'+sort+'">'
		    			+'<p class="up_img_title">排序：<b id="op_sort_'+sproid+'_'+sort+'">'+sort+'</b></p>'
		    			+'<p class="up_img_p"><img src="'+json.src+'" id="op_'+sproid+'_'+sort+'"/></p>'
		    	  		+'<div class="up_img_oper" style="display:none;"><a href="javascript:popEditOPSort('+sproid+','+sort+');">修改排序</a>'
		    	  		+'<a href="javascript:delOtherPhoto('+sproid+','+sort+');">删除</a></div>'
		    		    +'</div>';
					$('#op_imgs_'+sproid).append(html);
					closeUpImg();
					alert('添加成功');
				}else if(json.code == 2){
					alert('排序已经存在');
				}else{
					alert('添加失败');
				}
			}
		});
	}
}



function popEditOPSort(sproid,sort){
	if(sproid > 0 && sort > 1){
		   var html = '排　序：<input type="text" maxlength="2" size="4" id="photo_sort"><br/>大于2,且与同属性下排序不重复'
		   +'<br/><input class="button" type="button" value="修改 " onclick="editOPSort('+sproid+','+sort+');">';
	 	popDailog('排序修改',html,150);
	}
}

function editOPSort(sproid,oldsort){
	if(!Fun.zIntCheck($('#photo_sort')) && parseInt($('#photo_sort').val()) < 2){
		alert('排序必须为大于2的整数');
		return;
	}
	if(sproid > 0 && oldsort > 1){
		var newsort = parseInt($('#photo_sort').val());
		if(newsort == oldsort){
			alert('与原排序相同');
			return;
		}
		if($('#op_div_'+sproid+'_'+newsort)[0]){
			alert('此排序同属性下已存在');
			return;
		}
		var url = '/sys-admin/action/goods/product.php?met=editotherphotosort'
		       +'&sproid='+sproid
		       +'&oldsort='+oldsort
		       +'&newsort='+newsort
		       +'&t='+(new Date().getTime());
		$.get(url,null,function(result){
			result = $.trim(result);
			if(result == '1'){
				//获取前面一个div
				var prevPhoto = $('#op_div_'+sproid+'_'+oldsort).prev('div');
				var src =  $('#op_'+sproid+'_'+oldsort).attr('src');
				var html = '<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);" id="op_div_'+sproid+'_'+newsort+'">'
	    			+'<p class="up_img_title">排序：<b id="op_sort_'+sproid+'_'+newsort+'">'+newsort+'</b></p>'
	    			+'<p class="up_img_p"><img src="'+src+'" id="op_'+sproid+'_'+newsort+'"/></p>'
	    	  		+'<div class="up_img_oper" style="display:none;"><a href="javascript:popEditOPSort('+sproid+','+newsort+');">修改排序</a>'
	    	  		+'<a href="javascript:delOtherPhoto('+sproid+','+newsort+');">删除</a></div>'
	    		    +'</div>';
				$('#op_div_'+sproid+'_'+oldsort).remove();
				$(prevPhoto).after(html);
				closeUpImg();
				alert('排序修改成功');
			}else if(result == '2'){
				alert('此排序同属性下已存在');
			}else{
				alert('排序修改失败');
			}
		});
	}else{
		closeUpImg();
	}
}
function delOtherPhoto(sproid,sort){
	if(sproid > 0 && sort > 1){
		if(confirm("确定要删除此相册图?")){
			var url = '/sys-admin/action/goods/product.php?met=delotherphoto'
			       +'&sproid='+sproid
			       +'&sort='+sort
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('#op_div_'+sproid+'_'+sort).remove();
					alert('相册图删除成功');
				}else{
					alert('相册图删除失败');
				}
			});
		}
	}
}
function popUpSizeImg(proid){
	if(proid > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="up_size_img">'
			   +'<input type="hidden" name="met" value="upsizeimg"/>'
			   +'<input type="hidden" name="proid" value="'+proid+'"/>'
			   +'正面图： <input type="file" name="image" id="img"/>'
			   +'<br/><input class="button" type="button" value="添加 " onclick="upSizeImg();">'
			   +'</form>';
		 	popDailog('相册图片添加',html,150);
	}
}

function upSizeImg(){
	if(!Fun.isEmpty($('#img'))){
		alert('图片不能为空');
		return;
	}
	$('#up_size_img').ajaxSubmit({
		success:function(html){
			html = $.trim(html);
			var json = eval('('+html+')');
			if(json.code == 1){
				$('#p_size_img').attr('src',json.src)
				closeUpImg();
				alert('尺码图上传成功');
			}else{
				alert('尺码图上传失败');
			}
		}
	});
}


function delSizeImg(proid){
	if(proid > 0 && $('#p_size_img').attr('src') != ''){
		if(confirm("确定要删除尺码图?")){
			var url = '/sys-admin/action/goods/product.php?met=delsizeimg'
			       +'&proid='+proid
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('#p_size_img').attr('src','');
					alert('尺码图删除成功');
				}else{
					alert('尺码图删除失败');
				}
			});
		}
	}
}

function popAddProImg(proid){
	if(proid > 0){
		var html = '<form action="/sys-admin/action/goods/product.php" method="post" enctype="multipart/form-data" id="add_pro_img">'
		   +'<input type="hidden" name="met" value="addproimg"/>'
		   +'<input type="hidden" name="proid" value="'+proid+'"/>'
		   +'详情图： <input type="file" name="image" id="p_img"/>'
		   +'<p>排　序：<input type="text" name="sort" maxlength="2" size="4" id="img_sort"></p>'
		   +'<input class="button" type="button" value="添加 " onclick="addProImgs();">'
		   +'</form>';
	 	popDailog('详情展示图添加',html,150);
	}
}

function addProImgs(){
	if(!Fun.zIntCheck($('#img_sort'))){
		alert('排序必须为正整数');
		return;
	}
	if(!Fun.isEmpty($('#p_img'))){
		alert('图片不能为空');
		return;
	}
	$('#add_pro_img').ajaxSubmit({
		success:function(html){
			html = $.trim(html);
			var json = eval('('+html+')');
			if(json.code == 1){
				var html = '<div class="up_img" onmouseenter="enUpImg(this);" onmouseleave="leUpImg(this);" id="p_img_'+json.id+'">'
		    		+'<p class="up_img_title">排序：<b id="i_sort_'+json.id+'">'+$('#img_sort').val()+'</b></p>'
		    		+'<p class="up_img_p"><img src="'+json.src+'"/></p>'
		    	  	+'<div class="up_img_oper" style="display:none;"><a href="javascript:popEditImgSort('+json.id+');">修改排序</a><a href="javascript:delProImg('+json.id+');">删除</a></div>'
		    		+'</div>';
				$('#p_imgs').append(html);
				closeUpImg();
				alert('详情展示图添加成功');
			}else{
				alert('详情展示图添加失败');
			}
		}
	});
}

function delProImg(imgid){
	if(imgid > 0){
		if(confirm("确定要删除详情展示图?")){
			var url = '/sys-admin/action/goods/product.php?met=delproimg'
			       +'&imgid='+imgid
			       +'&t='+(new Date().getTime());
			$.get(url,null,function(result){
				result = $.trim(result);
				if(result == '1'){
					$('#p_img_'+imgid).remove();
					alert('详情展示图删除成功');
				}else{
					alert('详情展示图删除失败');
				}
			});
		}
	}
}

function popEditImgSort(imgid){
	if(imgid > 0){
		 var html = '排　序：<input type="text" maxlength="2" size="4" id="img_sort">'
			   +'<br/><input class="button" type="button" value="修改 " onclick="editImgSort('+imgid+');">';
		 	popDailog('详情展示图排序修改',html,150);
	}
}


function editImgSort(imgid){
	if(imgid > 0){
		if(!Fun.zIntCheck($('#img_sort'))){
			alert('排序必须为正整数');
			return;
		}
		var sort = parseInt($('#img_sort').val());
		var url = '/sys-admin/action/goods/product.php?met=editimgsort'
		       +'&imgid='+imgid
		       +'&sort='+sort
		       +'&t='+(new Date().getTime());
		$.get(url,null,function(result){
			result = $.trim(result);
			if(result == '1'){
				$('#i_sort_'+imgid).html(sort);
				closeUpImg();
				alert('详情展示图排序修改成功');
			}else{
				alert('详情展示图排序修改失败');
			}
		});
	}else{
		closeUpImg();
	}
}

function delStock(stockid,num){
	if(stockid > 0){
		var name = $('#p_st_'+stockid).find('b').html();
		if(num > 0){
			alert('库存\''+name+'\'的数量大于0,无法删除');
		}else{
			if(confirm("确定要删除库存'"+name+"'?")){
				var url = '/sys-admin/action/goods/product.php?met=delstock'
				       +'&stockid='+stockid
				       +'&t='+(new Date().getTime());
				$.get(url,null,function(result){
					result = $.trim(result);
					if(result == '1'){
						$('#p_st_'+stockid).remove();
						closeUpImg();
						alert('库存\''+name+'\'删除成功');
					}else if(result == '2'){
						alert('库存\''+name+'\'的数量大于0,无法删除');
					}else{
						alert('库存\''+name+'\'删除失败');
					}
				});
			}
		}
	}
}




